﻿/*****************************************************************************
	SNEGA STG GAME ENGINE 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/sng-actionscript-stg-engine/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package com.sng.stg.game.bullet 
{
	import com.sng.stg.game.enemy.Enemy;
	import com.sng.stg.game.StageViewer;
	import flash.display.*;
	
	/**
	 * ...
	 * @author snega
	 */
	public class PlayerBullet extends Bitmap
	{
		public var _stage:StageViewer;
		
		public var _previous:PlayerBullet = null;
		public var _next:PlayerBullet = null;
		
		public function PlayerBullet (stage:StageViewer, bitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false)
		{
			super(bitmapData, pixelSnapping, smoothing);
			
			this._stage = stage;
		}
		
		public function push(o:PlayerBullet):void {
			if (this._next == null) {
				o._previous = this;
				
				this._next = o;
			}
			else {
				this._next._previous = o;
				
				o._next = this._next;
				o._previous = this;
				
				this._next = o;
			}
		}
		
		public function remove():void {
			if (this._next != null) {
				this._next._previous = this._previous;
			}
			
			if (this._previous != null) {
				this._previous._next = this._next;
			}
		}
		
		public function process():void {			
			// check enemy health
			var enemy:Enemy = this._stage._currEnemyList._next;
			while (enemy != null) {
				// check hit
				if (this.y > enemy.y - enemy._height && this.y - 16 < enemy.y + enemy._height && this.x < enemy.x + enemy._width && this.x + 8 > enemy.x - enemy._width) {
					if (--enemy._life == 0) {
						this._stage._player._point += 100;
						enemy.kill();
					}
					
					this.remove();
					this._stage._2dElement.removeChild(this);
					
					break;
				}
				
				// next
				enemy = enemy._next;
			}
			
			// move bullet
			this.y -= 20;
		}
		
	}
	
}